package com.woniu.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniu.utils.BaseController;
import com.woniu.entity.Auctiongoods;
import com.woniu.entity.Finance;
import com.woniu.entity.Goods;
import com.woniu.entity.Order;
import com.woniu.entity.Record;
import com.woniu.model.Funds;
import com.woniu.service.FinanceService;
import com.woniu.utils.PageUtil;

@Controller
@RequestMapping("/auction/Finance")
public class FinanceController extends BaseController {

	@Autowired
	private FinanceService financeService;

	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(String orderGenerateId, String buyUserName, Date startTime, Date endTime, int limit,
			int page, HttpServletResponse req) throws IOException {
		req.setCharacterEncoding("utf-8");
		PageUtil myPage = financeService.selectAllByIdAnduNameAndTime(orderGenerateId, buyUserName, startTime, endTime,
				limit, page);
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("code", 0);
		result.put("msg", "");
		result.put("count", myPage.getCount());
		result.put("data", myPage.getList());
		return result;
	}

	@RequestMapping("funds")
	@ResponseBody
	public Funds selectFunds(Date startTime, Date endTime) {
		return financeService.selectFunds(startTime, endTime);
	}

	@Scheduled(fixedDelay = 6000000)
	@Async
	public void insertFinance() {
		List<Order> orderList = financeService.selectOrderList();
		for (Order f : orderList) {
				if ("3".equals(f.getOrderstatus())) {
					String ordergenerateid = f.getOrdergenerateid();
					Integer gid = f.getGid();
					Date dealtime = f.getDealtime();
					double dealprice = f.getDealprice();
					double commission = f.getCommision();
					double totalrevenue = f.getOrdertotalprice();
					Integer uBid = f.getuBid();
					Integer sSid = f.getuSid();
					String paymentchannel = f.getPaymentchannel();
					financeService.insertFinance(ordergenerateid, gid, dealtime, dealprice, commission, totalrevenue,
							uBid, sSid, paymentchannel);
				}
			}
		
	}

}